package com.system.uitls;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.LinkedHashSet;

import org.springframework.core.io.ClassPathResource;

public class FileUtils
{
  public static String basePath = System.getProperty("user.dir") + File.separator;

  public static boolean appendText(String path, String text)
  {
    try
    {
      File file = new File(path);

      BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
      writer.write(text);
      return true;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    return false;
  }

  public static void setBasePath(String basePath)
  {
    if (!basePath.trim().endsWith(File.separator))
      basePath = basePath + File.separator;
    basePath = basePath;
  }

  public static String getAbsolutePath(String path)
  {
    if (path.contains(":"))
    {
      return path;
    }
    if ((path != null) && (path.trim().length() == 1))
    {
      return basePath;
    }
    if (path.startsWith("/"))
    {
      path = path.substring(1);
    }
    path = basePath + path.replace("/", File.separator);
    return path;
  }

  public static void copyFile(File inFile, File outFile) {
    try {
      copyFile(new FileInputStream(inFile), outFile);
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
  }

  public static void copyFile(InputStream in, File outFile) {
    OutputStream out = null;
    try {
      byte[] data = readAll(in);
      out = new FileOutputStream(outFile);
      out.write(data, 0, data.length);
      out.close();
    } catch (Exception ex) {
      ex.printStackTrace();
      try
      {
        if (in != null)
          in.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (out != null)
          out.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    finally
    {
      try
      {
        if (in != null)
          in.close();
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }
      try {
        if (out != null)
          out.close();
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }

  public static byte[] readAll(File file) {
    try {
      return readAll(new FileInputStream(file));
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public static byte[] readAll(InputStream in) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
      byte[] buffer = new byte[1024];
      int n;
      while ((n = in.read(buffer)) > 0)
      {
        out.write(buffer, 0, n);
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return out.toByteArray();
  }

  public static FileInputStream getInputStream(String path) {
    try {
      return new FileInputStream(getAbsolutePath(path));
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
    return null;
  }
  public static boolean existsFile(String path) {
    try {
      File file = new File(getAbsolutePath(path));
      if (file.exists())
        return true;
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return false;
  }
  public static File createAndWriteFile(String path, byte[] data) {
    try {
      File file = new File(getAbsolutePath(path));
      if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
      }
      file.createNewFile();
      OutputStream out = new FileOutputStream(file);
      out.write(data, 0, data.length);
      return file;
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }
  public static File createAndWriteFile(String path, String text) {
    try {
      File file = new File(getAbsolutePath(path));
      if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
      }
      file.createNewFile();
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
      writer.write(text);
      return file;
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public static boolean removeFile(String path) {
    try {
      File file = new File(getAbsolutePath(path));
      if (file.exists()) {
        file.delete();
      }
      return true;
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return false;
  }

  public static Collection<String> getTextFileContent(String path) {
    return getTextFileContent(getInputStream(path));
  }

  public static Collection<String> getTextFileContent(InputStream in) {
    Collection result = new LinkedHashSet();
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
      String line = reader.readLine();
      while (line != null)
      {
        if ((!"".equals(line.trim())) && (!line.trim().startsWith("#"))) {
          result.add(line);
        }
        line = reader.readLine();
      }
    } catch (UnsupportedEncodingException ex) {
      ex.printStackTrace();
      try
      {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    catch (IOException ex)
    {
      ex.printStackTrace();
      try
      {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    finally
    {
      try
      {
        reader.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    return result;
  }
  public static Collection<String> getClassPathTextFileContent(String path) {
    try {
      ClassPathResource cr = new ClassPathResource(path);
      return getTextFileContent(cr.getInputStream());
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return null;
  }
}